package com.example.administrator.zhongyichou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.zhongyichou.help.ImageLoader;
import com.example.administrator.zhongyichou.help.StringImageToList;
import com.example.administrator.zhongyichou.net.protocol.MutualProtocol;
import com.example.administrator.zhongyichou.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lichenyu on 2017/11/20.
 * 描述：
 */

public class MutualAidAdapter extends ListBaseAdapter {

    private OnItemClick onItemClick;

    public MutualAidAdapter(Context mContext, List list) {
        super(mContext, list);
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.mutual_aid_list_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        MutualProtocol.DataBean mutualProtocol = (MutualProtocol.DataBean) getData(position);
        if ("1".equals(mutualProtocol.getJoin_type())) {
            holder.btnJoin.setBackgroundResource(R.mipmap.help_btn_gradient_red);
            holder.btnJoin.setText("立即升级");
        } else {
            holder.btnJoin.setBackgroundResource(R.mipmap.help_btn_gradient);
            holder.btnJoin.setText("立即加入");
        }
        holder.txtTitle.setText(mutualProtocol.getType_title());
        holder.txtNum.setText(mutualProtocol.getMember_num());
        ImageLoader.centerCropUrl(getContext(), StringImageToList.toImage(mutualProtocol.getType_img()), holder.imgPublicWelfare);
        holder.txtPublicWelfareDes.setText(mutualProtocol.getMin_age() + "-" + mutualProtocol.getMax_age() + "周岁");
        holder.txtPublicWelfareDes1.setText("最高可得" + mutualProtocol.getMax_amount() + "万互助金");
        holder.txtTextPublicWelfareDes2.setText(mutualProtocol.getDisease_desc());
        holder.btnJoin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick.onClick(position);
            }
        });
        switch (position) {
            case 0:
                ImageLoader.centerCropRes(getContext(), R.mipmap.banner_forever, holder.imgPublicWelfare);
                break;
            case 1:
                ImageLoader.centerCropRes(getContext(), R.mipmap.banner_children, holder.imgPublicWelfare);
                break;
            case 2:
                ImageLoader.centerCropRes(getContext(), R.mipmap.banner_youth, holder.imgPublicWelfare);
                break;
            case 3:
                ImageLoader.centerCropRes(getContext(), R.mipmap.banner_old, holder.imgPublicWelfare);
                break;
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_num)
        TextView txtNum;
        @BindView(R.id.img_public_welfare)
        ImageView imgPublicWelfare;
        @BindView(R.id.txt_public_welfare_des)
        TextView txtPublicWelfareDes;
        @BindView(R.id.ll_public_welfare_des)
        LinearLayout llPublicWelfareDes;
        @BindView(R.id.txt_public_welfare_des1)
        TextView txtPublicWelfareDes1;
        @BindView(R.id.ll_public_welfare_des1)
        LinearLayout llPublicWelfareDes1;
        @BindView(R.id.txt_text_public_welfare_des2)
        TextView txtTextPublicWelfareDes2;
        @BindView(R.id.ll_public_welfare_des2)
        LinearLayout llPublicWelfareDes2;
        @BindView(R.id.btn_join)
        Button btnJoin;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClick {
        void onClick(int position);
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }
}
